#!/bin/sh

echo "[extensions]" >> $HGRCPATH
echo "nearest=$NEAREST" >> $HGRCPATH
echo "graphlog=" >> $HGRCPATH

HGMERGE=true
export HGMERGE

addrev() {
  if [ "$#" -ne 2 ]; then
    echo "ERROR: test script internal error"
    exit 1
  fi
  if [ $1 != $lastrev ]; then 
    hg -q update -C $1
  fi
  echo dummy >> file
  hg -q commit --message $2 --date "`expr $2 \* 86400 + $2` 0"
  lastrev=$2
  if [ `hg id -n` -ne $lastrev ]; then
      echo "ERROR: test script internal error `hg id -n` != $lastrev"
    exit 1
  fi
}

mergerev () {
  if [ "$#" -ne 3 ]; then
    echo "ERROR: test script internal error"
    exit 1
  fi
  if [ $1 != $lastrev ]; then 
    hg -q update -C $1
  fi
  hg -q merge $2
  hg -q commit --message $3 --date "`expr $3 \* 86400 + $3` 0"
  lastrev=$3
  if [ `hg id -n` -ne $lastrev ]; then
      echo "ERROR: test script internal error `hg id -n` != $lastrev"
    exit 1
  fi
}

# init repo
hg init repo
cd repo
echo dummy > file
hg add file
hg ci -m 0
lastrev=0

addrev 0 1
addrev 1 2
addrev 1 3
addrev 3 4
mergerev 4 2 5
addrev 5 6
addrev 5 7
addrev 7 8
mergerev 6 8 9
addrev 9 10
addrev 10 11
addrev 9 12
mergerev 11 12 13
addrev 13 14
addrev 14 15
addrev 13 16
mergerev 16 15 17
addrev 17 18

hg glog --style compact

### first, test ordering by date
echo '% test without contains'
# no tag, no output
hg nearest -l
echo '% simple tag after merges'
hg tag -l -r 1 t1
hg nearest -l

echo '% tag on one branch, more hops but closer date'
hg tag -l -r 2 t2
hg nearest -l
hg tag -l -r 3 t3
hg nearest -l

echo '% stop at 5'
hg tag -l -r 5 t5
hg nearest -l

echo '% tag on one branch, more hops but closer date'
hg tag -l -r 6 t6
hg nearest -l
hg tag -l -r 7 t7
hg nearest -l

echo '% stop at 9'
hg tag -l -r 9 t9
hg nearest -l

echo '% tag on one branch, equal hops, diff dates'
hg tag -l -r 11 t11
hg nearest -l
hg tag -l -r 12 t12
hg nearest -l

echo '% stop at 13'
hg tag -l -r 13 t13
hg nearest -l

echo '% tag on one branch, equal hops, diff dates'
hg tag -l -r 15 t15
hg nearest -l
hg tag -l -r 16 t16
hg nearest -l

echo
hg tag --local --remove t1 t2 t3 t5 t6 t7 t9 t11 t12 t13 t15 t16

echo '% reversing the tree walk'
hg nearest -l 0
# no tag, no output
hg tag -l -r 17 t17
hg nearest -l 0

echo '% tag on one branch, more hops but closer date'
hg tag -l -r 16 t16
hg nearest -l 0
hg tag -l -r 15 t15
hg nearest -l 0

echo '% stop at 13'
hg tag -l -r 13 t13
hg nearest -l 0

echo '% tag on one branch, more hops but closer date'
hg tag -l -r 12 t12
hg nearest -l 0
hg tag -l -r 11 t11
hg nearest -l 0

echo '% stop at 9'
hg tag -l -r 9 t9
hg nearest -l 0

echo '% tag on one branch, equal hops, diff dates'
hg tag -l -r 7 t7
hg nearest -l 0
hg tag -l -r 6 t6
hg nearest -l 0

echo '% stop at 5'
hg tag -l -r 5 t5
hg nearest -l 0

echo '% tag on one branch, equal hops, diff dates'
hg tag -l -r 3 t3
hg nearest -l 0
hg tag -l -r 2 t2
hg nearest -l 0

### test exclude mq tags
echo
echo '% exclude mq tags'
echo 'hgext.mq=' >> $HGRCPATH
hg qnew p1
echo p1 >> file
hg qrefresh
hg qnew p2
echo p2 >> file
hg qrefresh
hg nearest -l
hg qpop -a

### Now test for multiple tags reporting
echo
echo '% multiple tags'
hg tag --local --remove t17
hg tag -r 16 -d "`expr 19 \* 86400 + 19` 0" -m t16.1 tag16.1
hg tag -r 16 -d "`expr 20 \* 86400 + 20` 0" -m t16.2 tag16.2
hg tag -r 15 -d "`expr 21 \* 86400 + 21` 0" -m t15.1 tag15.1
hg nearest
echo '% see them all'
hg nearest -l
echo '% exact match'
hg nearest 15
echo '% see them all'
hg nearest -l 16

### test for match
echo '% testing --match t*.? from tip'
hg nearest --match 't*.?'
echo '% testing --match t*.? from 0'
hg nearest --match 't*.?' 0
echo '% testing --match x* from tip'
hg nearest --match 'x*' 0
### test for regexp
echo '% testing --regexp [0-9]\.[0-9] from tip'
hg nearest --regexp '[0-9]\.[0-9]'
echo '% testing --regexp [0-9]\.[0-9] from 0'
hg nearest --regexp '[0-9]\.[0-9]' 0
echo '% testing --regexp x.* from 0'
hg nearest --regexp 'x.*' 0

